﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;

namespace BLL
{
  public class UserBLL
  {
    //Get all
    public List<User> GetAll()
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Users.OrderByDescending(p => p.Id).ToList();
      }
    }

    public List<User> GetAll(bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Users.Where(p => p.isEnable == isEnable).OrderByDescending(p => p.Id).ToList();
      }
    }

    //Get by Role Id

    public List<User> GetByRoleId(int roleId)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Users.Where(p => p.RoleId == roleId).OrderByDescending(p => p.Id).ToList();
      }
    }

    public List<User> GetByRoleId(int roleId, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Users.Where(p => p.RoleId == roleId && p.isEnable == isEnable).OrderByDescending(p => p.Id).ToList();
      }
    }

    //Get by Id

    public User GetById(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Users.Where(p => p.Id == id).FirstOrDefault();
      }
    }

    public User GetById(int id, bool isEnable)
    {
      using (var data =new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Users.Where(p => p.Id == id && p.isEnable == isEnable).FirstOrDefault();
      }
    }

    public User GetByUserName(string userName)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Users.Where(p => p.UserName == userName && p.isEnable == true).FirstOrDefault();
      }
    }
    //Enable

    public void Enable(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        User user = data.Users.Where(p => p.Id == id).FirstOrDefault();
        user.isEnable = !user.isEnable;

        data.SubmitChanges();
      }
    }

    public void Enable(int id, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        User user = data.Users.Where(p => p.Id == id).FirstOrDefault();
        user.isEnable = isEnable;

        data.SubmitChanges();
      }
    }


    //Check exist user

    public bool isExitsUserName(string userName)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        User obj = data.Users.Where(p => p.UserName == userName).FirstOrDefault();
        if (obj == null) return false;
        else return true;
      }
    }

    //Check login

    public bool isCheckLogin(string userName, string pass)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        User obj = data.Users.Where(p => p.UserName == userName && p.PassWord == pass).FirstOrDefault();
        if (obj == null) return false;
        else return true;
      }
    }
   
    //Change pass

    public void ChangePass(string userName, string newPass)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        User user = data.Users.Where(p => p.UserName == userName).FirstOrDefault();
        user.PassWord = newPass;

        data.SubmitChanges();
      }
    }

    //Add

    public void Add(User obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        data.Users.InsertOnSubmit(obj);
        data.SubmitChanges();
      }
    }

    //Update

    public void Update(User obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        User user = data.Users.Where(p => p.Id == obj.Id).FirstOrDefault();
        user.CreatedDate = obj.CreatedDate;
        user.Email = obj.Email;
        user.isEnable = obj.isEnable;
        user.Name = obj.Name;
        user.PassWord = obj.PassWord;
        user.Phone = obj.Phone;
        user.RoleId = obj.RoleId;
        user.UserName = obj.UserName;

        data.SubmitChanges();
      }
    }

    //Delete

    public void Delete(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        User obj = data.Users.Where(p => p.Id == id).FirstOrDefault();
        if (obj != null)
          data.Users.DeleteOnSubmit(obj);

        data.SubmitChanges();
      }
    }
  }
}
